package dao;

import beans.Insumo;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class InsumoDAO {
    
    PreparedStatement pst;
    ResultSet rs;
    
    public InsumoDAO() {
    }
    
    public Insumo load(int id) {
        Insumo ins = new Insumo();

        String query = "SELECT * FROM insumo WHERE id = ?";

        try {
            pst = Conexao.getConexao().prepareStatement(query);
            pst.setInt(1, id);

            rs = pst.executeQuery();

            if (rs.next()) {
                ins.setId(rs.getInt("id"));
                ins.setItem(rs.getString("item"));
                ins.setDataVencimento(rs.getDate("dataVencimento"));
                ins.setUnidade(rs.getString("unidade"));
                ins.setQuantidade(rs.getInt("quantidade"));
                ins.setIdCompra(rs.getInt("idCompra"));
            }

        } catch (SQLException ex) {
            ex.printStackTrace();
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (pst != null) {
                    pst.close();
                }
            } catch (SQLException ex) {
                            ex.printStackTrace();
            }
        }

        return ins;
    }
 
     public void insert(Insumo ins) {
        String query = "INSERT INTO insumo (id, item, dataVencimento, unidade, quantidade, idCompra)"
                + " VALUES(?, ?, ?, ?, ?, ?)";

        try {
            pst = Conexao.getConexao().prepareStatement(query);

            pst.setInt(1, ins.getId());
            pst.setString(2, ins.getItem());
            pst.setDate(3, ins.getDataVencimento());
            pst.setString(4, ins.getUnidade());
            pst.setInt(5, ins.getQuantidade());
            pst.setInt(6, ins.getIdCompra());

            pst.executeUpdate();

        } catch (Exception ex) {
                                        ex.printStackTrace();
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (pst != null) {
                    pst.close();
                }
            } catch (SQLException ex) {
                            ex.printStackTrace();
            }
        }
    }

     private void update(Insumo ins) {
        String query = "UPDATE insumo"
                + "SET item = ?, dataVencimento = ?, unidade = ?, quantidade = ? "
                + "WHERE id = ?";

        try {
            pst = Conexao.getConexao().prepareStatement(query);

            pst.setInt(1, ins.getId());
            pst.setDate(2, ins.getDataVencimento());
            pst.setString(3, ins.getUnidade());
            pst.setInt(4, ins.getQuantidade());

            pst.executeUpdate();

        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (pst != null) {
                    pst.close();
                }
            } catch (SQLException ex) {
                ex.getLocalizedMessage();
            }
        }


    }

     public void save(Insumo ins) {
        String query = "SELECT COUNT(*) FROM insumo WHERE id = ?";

        try {
            pst = Conexao.getConexao().prepareStatement(query);
            pst.setInt(1, ins.getId());

            rs = pst.executeQuery();
            rs.next();

            if (rs.getInt("count") != 0) {
                insert(ins);
            }

        } catch (SQLException ex) {
            ex.printStackTrace();

        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (pst != null) {
                    pst.close();
                }
            } catch (SQLException ex) {
                ex.getLocalizedMessage();
            }
        }
    }

     public void delete(Insumo ins) {
        String query = "DELETE FROM insumo WHERE id = ?";

        try {
            pst = Conexao.getConexao().prepareStatement(query);
            pst.setInt(1, ins.getId());

            pst.executeUpdate();

        } catch (SQLException ex) {
            ex.printStackTrace();

        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (pst != null) {
                    pst.close();
                }
            } catch (SQLException ex) {
                ex.getLocalizedMessage();
            }
        }
    }

     public ArrayList<Insumo> loadAll() {
        ArrayList<Insumo> insumos = new ArrayList<Insumo>();

        String query = "SELECT * FROM insumo";

        try {
            pst = Conexao.getConexao().prepareStatement(query);
            rs = pst.executeQuery();

            while (rs.next()) {
                Insumo ins = new Insumo();

                ins.setId(rs.getInt("id"));
                ins.setItem(rs.getString("item"));
                ins.setDataVencimento(rs.getDate("data"));
                ins.setUnidade(rs.getString("unidade"));
                ins.setQuantidade(rs.getInt("quantidade"));
                ins.setIdCompra(rs.getInt("idCompra"));

                insumos.add(ins);
            }
        } catch (SQLException ex) {
            ex.getLocalizedMessage();
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (pst != null) {
                    pst.close();
                }
            } catch (SQLException ex) {
                ex.getLocalizedMessage();
            }
        }

        return insumos;
    }
}
